const db = require('../db/mysqlconfig')
var bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const privateKey = require('../tokenprivatekey/key')
exports.login = (req, res) => {
    const userinfo = req.body

    const sql = 'select * from sp_manager where mg_name=?'
    db.query(sql, userinfo.username, (err, resualts) => {
            if (err) return res.ResInfo(err)
            if (resualts.length != 1) return res.ResInfo('用户名错误')

            const compareres = bcrypt.compareSync(userinfo.password, resualts[0].mg_pwd)
            if (!compareres) return res.ResInfo('密码错误')
            const user = {...resualts[0], mg_pwd: '' }
            var token = jwt.sign(user, privateKey.key, { expiresIn: '1h' });
            res.ResInfo('登陆成功', 200, { token: 'Bearer ' + token })
        })
        // res.send({ status: 0, message: "登陆成功", data: req.body })
}